<?xml version="1.0"   encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC   "-//mybatis.org//DTD Mapper 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package_dao}.${Table}Dao">

        <!--${TableName}表和${Table}${pojoSuffix}实体类的映射关系-->
        <resultMap id="${Table}${pojoSuffix}ResultMap"    type="${package_pojo}.${Table}${pojoSuffix}">
                <#if mapId["t_id"]??!=false&&mapId["o_id"]??!=false>
                <id column="${mapId["t_id"]}"    property="${mapId["o_id"]}"/>
                </#if>
                <#list mapperXml?keys as k>
                <result column="${k}"    property="${mapperXml[k]}"/>
                </#list>
        </resultMap>


        <!-- ${TableName}表的所有列名 包括主键 -->
        <sql id="Column">
                ${columnALL}
        </sql>

        <!-- ${TableName}表的所有列名 不包括主键 -->
        <sql id="ColumnNotId">
                ${columnALLNotId}
        </sql>

<!--                              + + + +逆向自动生成的查询只能满足简单的查询功能,在复杂的环境下还需要自己手写                       -->
        <!--查询${TableName}表数据的总行数-->
        <select id="select${Table}Count"   resultType="Integer">
                SELECT COUNT(*) FROM `${TableName}`
        </select>


        <!--查询${TableName}表的全部数据集合-->
        <select id="select${Table}s"   resultType="${package_pojo}.${Table}${pojoSuffix}" >
                SELECT  <include refid="Column"/>  FROM  `${TableName}`
        </select>


        <!--查询${TableName}表的主键id对应的值-->
        <select id="select${Table}ById" parameterType="${keyType}"  resultType="${package_pojo}.${Table}${pojoSuffix}" >
                SELECT  <include refid="Column"/>  FROM  `${TableName}`
                WHERE  `${mapId["t_id"]}`=${"#{"+mapId["o_id"]+"}"}
        </select>



        <!--查询${TableName}表的指定条件(${Table}${pojoSuffix})的值 (包含: 自定义排序 ,自定义分页,自定义列)-->
        <!-- 自定义列: 自己添加需要查询的字段,或者需要聚合的函数  比如  COUNT(id)  SUM(age)  AVG(age)-->
        <!-- 排序比如:  ORDER BY  id  DESC  这段代码意思就是安装id从大到小排序 (DESC倒序)  (ASC正序)   (注意:  ORDER BY 已经帮您写了,你字需要添加字段就行)-->
        <!-- 分页比如:  startRow(起始)  和  rowSize(查询多少条)    如果只是rowSize那么就是查询几条-->
        <select id="select${Table}By${Table}" parameterType="${package_pojo}.${Table}${pojoSuffix}"  resultType="${package_pojo}.${Table}${pojoSuffix}">
                SELECT
                    <choose>
                    <when test="condition!=null and condition.column != null and condition.column != ''">
                            ${"${"+"condition.column"+"}"}
                    </when>
                    <otherwise>
                    <include refid="Column"/>
                    </otherwise>
                    </choose>
                  FROM  `${TableName}`

                <where>
                    <#list mapperXml?keys as k>
                            <if test="${mapperXml[k]} != null">
                               AND `${k}`=${"#{"+mapperXml[k]+"}"}
                            </if>
                    </#list>
                </where>


            <if test="condition!=null">
                <if test="condition.order !=null and condition.order != ''"> GROUP BY ${"$"+"{"+"condition.order"+"}"}</if>

                <choose>
                        <when test="condition.startRow !=null and condition.rowSize !=null">LIMIT ${"#{"+"condition.startRow"+"}"},${"#{"+"condition.rowSize"+"}"} </when>
                        <when test="condition.rowSize !=null">LIMIT ${"#{"+"condition.rowSize"+"}"}</when>
                </choose>
            </if>
        </select>


        <!-- 查询${TableName}表的指定条件(${Table}${pojoSuffix})的值,自定义查询(包含: 自定义列,自定义查询条件 , 自定义分组(分组后的赛选),自定义排序 ,自定义分页)-->
        <!-- 自定义列: 自己添加需要查询的字段,或者需要聚合的函数  比如  COUNT(id)  SUM(age)  AVG(age)-->
        <!-- 自定义查询条件:  where 不用写了其他的需要自己补充和写sql一样-->
        <!-- 指定需要分组的字段  比如:   GROUP BY id   (注意: GROUP BY 已经帮您写了,你字需要添加字段就行)-->
        <!-- 分组后的赛选 比如:  HAVING id=1  (注意: HAVING 已经帮您写了,你字需要添加字段就行)-->
        <!-- 排序比如:  ORDER BY  id  DESC  这段代码意思就是安装id从大到小排序 (DESC倒序)  (ASC正序)   (注意:  ORDER BY 已经帮您写了,你字需要添加字段就行)-->
        <!-- 分页比如:  startRow(起始)  和  rowSize(查询多少条)    如果只是rowSize那么就是查询几条-->
        <select id="select${Table}ByCustom" parameterType="${package_pojo}.${Table}${pojoSuffix}"  resultType="${package_pojo}.${Table}${pojoSuffix}">

                SELECT
                    <choose>
                    <when test="condition!=null and condition.column != null and condition.column != ''">
                         ${"${"+"condition.column"+"}"}
                    </when>
                    <otherwise>
                        <include refid="Column"/>
                    </otherwise>
                    </choose>
                 FROM  `${TableName}`
             <if test="condition!=null">

                <if test="condition.where != null and condition.where != '' ">
                    <where>
                        ${"$"+"{"+"condition.where"+"}"}
                    </where>
                </if>

                <if test="condition.group !=null  and condition.group != '' ">
                         GROUP BY  ${"$"+"{"+"condition.group"+"}"}

                         <if test="condition.having !=null and condition.having != '' ">HAVING ${"$"+"{"+"condition.having"+"}"}</if>
                </if>

                  <if test="condition.order !=null and condition.order != '' "> ORDER BY ${"$"+"{"+"condition.order"+"}"}</if>

                <choose>
                        <when test="condition.startRow !=null and condition.rowSize !=null">LIMIT ${"#{"+"condition.startRow"+"}"},${"#{"+"condition.rowSize"+"}"} </when>
                        <when test="condition.rowSize !=null">LIMIT ${"#{"+"condition.rowSize"+"}"}</when>
                </choose>
            </if>
        </select>


<!--                             - - - -逆向自动生成的查询只能满足简单的查询功能,在复杂的环境下还需要自己手写                        -->




<!--                             + + + +逆向自动生成的插入只能满足简单的插入功能,在复杂的环境下还需要自己手写                        -->

                <!-- 插入一个${Table}${pojoSuffix}对象的数据到数据库${TableName}表中,包括属性值为null的数据  ,不包括主键因为一般主键都是自动生成的-->
                <insert id="insert${Table}By${Table}"  parameterType="${package_pojo}.${Table}${pojoSuffix}"  >
                       INSET INTO `${TableName}`
                        ( <include refid="ColumnNotId"/> )
                        values
                        ${insert}
                </insert>

                <!-- 插入一个${Table}${pojoSuffix}对象的数据到数据库${TableName}表中,不包括属性值为null的数据 和 不包括主键因为一般主键都是自动生成的-->
                <!-- 插入一个UserDO对象的数据到数据库t_user表中,包括属性值为null的数据  ,不包括主键因为一般主键都是自动生成的-->
                <insert id="insert${Table}By${Table}IsNotNull"  parameterType="${package_pojo}.${Table}${pojoSuffix}" >

                        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
                                select LAST_INSERT_ID()
                        </selectKey>

                        INSET INTO `${TableName}`
                        <trim prefix="(" suffix=")" suffixOverrides=",">
                                 <#list mapperXml?keys as k>
                                <if test="${mapperXml[k]} != null">
                                        ${"`"+k+"`"},
                                </if>
                                 </#list>
                        </trim>
                        <trim prefix="values (" suffix=")" suffixOverrides=",">
                                 <#list mapperXml?keys as k>
                                <if test="${mapperXml[k]} != null">
                                       ${"#{"+mapperXml[k]+"}"},
                                </if>
                                </#list>
                        </trim>
                </insert>



                <!-- 插入一批${Table}${pojoSuffix}对象的数据到数据库${TableName}表中,包括属性值为null的数据  ,不包括主键因为一般主键都是自动生成的-->
                <!-- 插入一个UserDO对象的数据到数据库t_user表中,包括属性值为null的数据  ,不包括主键因为一般主键都是自动生成的-->
                <insert id="batchInsert${Table}By${Table}"  parameterType="java.util.ArrayList"  >

                        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
                                select LAST_INSERT_ID()
                        </selectKey>
                        INSET INTO `${TableName}`
                        ( <include refid="ColumnNotId"/> )
                        values
                        <foreach collection="list" index="index" separator="," item="nextList">
                                <trim prefix="(" suffix=")" suffixOverrides=",">
                                        <#list mapperXml?keys as k>
                                               ${"#{"+"nextList."+mapperXml[k]+"}"},
                                        </#list>
                                </trim>
                        </foreach>
                </insert>



                <!-- 插入一批${Table}${pojoSuffix}对象的数据到数据库${TableName}表中,不包括属性值为null的数据 和 不包括主键因为一般主键都是自动生成的-->
                <insert id="batchInsert${Table}By${Table}NotNull"  parameterType="java.util.ArrayList" >
                        INSET INTO `${TableName}`
                        <trim prefix="(" suffix=")" suffixOverrides=",">
                                <#list mapperXml?keys as k>
                                        <if test="${mapperXml[k]} != null">
                                                ${"`"+k+"`"},
                                        </if>
                                </#list>
                        </trim>
                                values
                        <foreach collection="list" index="index" separator="," item="nextList">
                                <trim prefix="(" suffix=")" suffixOverrides=",">
                                        <#list mapperXml?keys as k>
                                                <if test="${mapperXml[k]} != null">
                                                        ${"#{"+"nextList."+mapperXml[k]+"}"},
                                                </if>
                                        </#list>
                                </trim>
                        </foreach>

                </insert>





<!--                              - - - -逆向自动生成的插入只能满足简单的插入功能,在复杂的环境下还需要自己手写                        -->







<!--                             + + + +逆向自动生成的修改只能满足简单的修改功能,在复杂的环境下还需要自己手写                        -->



                <!-- 根据主键ID修改${TableName}表中,括属性值为null的数据  -->
                <update id="update${Table}ById"  parameterType="${package_pojo}.${Table}${pojoSuffix}">
                            update `${TableName}`
                            <set>
                                <#list mapperXml?keys as k>
                                        ${"`"+k+"`"} = ${"#{"+mapperXml[k]+"}"},
                                </#list>
                            </set>
                            where  `${mapId["t_id"]}`=${"#{"+mapId["o_id"]+"}"}
                </update>



                <!-- 根据主键ID修改${TableName}表中,不包括属性值为null的数据  -->
                <update id="update${Table}ByIdNotNull"  parameterType="${package_pojo}.${Table}${pojoSuffix}">
                        update `${TableName}`
                        <set>
                               `${mapId["t_id"]}`=${"#{"+mapId["o_id"]+"}"},  -- 这一行是防止都为NULL时候引发的错误
                            <#list mapperXml?keys as k>
                                <if test="${mapperXml[k]} != null">
                                ${"`"+k+"`"} = ${"#{"+mapperXml[k]+"}"},
                                </if>
                            </#list>
                        </set>
                        where  `${mapId["t_id"]}`=${"#{"+mapId["o_id"]+"}"}
                </update>




<!--                            - - - -逆向自动生成的修改只能满足简单的修改功能,在复杂的环境下还需要自己手写                         -->









<!--                             + + + +逆向自动生成的删除只能满足简单的删除功能,在复杂的环境下还需要自己手写                        -->
            <!-- 根据主键ID删除${TableName}表中指定数据  -->
            <delete id="delete${Table}ById"  parameterType="Integer">
                    DELETE FROM `${TableName}` WHERE  `${mapId["t_id"]}`=${"#{"+mapId["o_id"]+"}"}
            </delete>

            <!-- 批量主键ID删除${TableName}表中指定数据  -->
            <delete id="batchDelete${Table}ById"  parameterType="Integer">
                    DELETE FROM `${TableName}` WHERE  `${mapId["t_id"]}` IN
                <foreach collection="list" item="nextList" open="(" separator="," close=")">
                     ${"#{"+"nextList"+"}"},
                </foreach>

            </delete>

        <!--清空${TableName}表的数据,主键自增也会重置  -->
            <delete id="clear${Table}All" >
                 TRUNCATE TABLE `${TableName}`
            </delete>

<!--                             - - - -逆向自动生成的删除只能满足简单的删除功能,在复杂的环境下还需要自己手写                        -->



</mapper>